# Asylum 0.0.2 - a refuge from the difficulties of getting the latest, greatest and best software! http://asylum.inc-omplete.org
# Copyright (c) 2006, Ian Caldwell (Inc-corporate.org)
# License: BSD license, see license.txt
# Purpose: Make svn builds of Songbird on linux easier
# Contact us: Inc@inc-corporate.org, or irc.freenode.net #asylum
#------------------------------------------------------------------------------------------------------------------

import sys
import math
import time
import os
import urllib2
import datetime
import subprocess
import asylum_base
def message():
	print "Asylum Build System V0.0.2"
	print "Copyright (c) 2006 Ian Caldwell. Licensed under the BSD license (see license.txt)"

def prompt_profile():
	pass

def use_profile(profile):
	pass

def main():
	message()	
	sUrltemplate = "http://developer.songbirdnest.com/nightly/snapshots/"
	sFiletemplate = "songbird_snapshot_%Y%m%d.tar.gz"
	sFile = datetime.date.today().strftime(sFiletemplate)
	sUrl = sUrltemplate + sFile
	sources = "The songbird source that is being grabbed is: "
	print sources + sUrl
	ui = asylum_base.ui()
	asylum_base.download([sUrl],sFile,ui)
	done = " has finished downloading now moving onto: "
	dstep = "\nDependency download"
	print sFile + done + dstep
	dUrltemplate = "http://developer.songbirdnest.com/nightly/dependency_snapshots/"
	dFiletemplate ="dependencies_snapshot-linux-i386-%Y%m%d.tar.gz"
	dFile = datetime.date.today().strftime(dFiletemplate)
	dUrl = dUrltemplate + dFile
	depend = "The Songbird dependencies file that is being grabbed is: "
	print depend + dUrl
	asylum_base.download([dUrl],dFile,ui)
	source = "source-decompression"
	depends = "dependency-decompression"
	tarstep = "Asylum is now uncompressing: " 
	print "\nhere comes uncompressing of the source this takes about 1 minute and 10 seconds. Please be patient"
	subprocess.call(['tar', '-xvzf', sFile,'-C', os.path.expanduser('~')], stdout=open('%s.logfile' % source, 'w') )
	print "ok, so here comes the big gun (extracting the dependencies).This process takes about 2 minutes and 30 seconds to complete.....so chill"
	subprocess.call(['tar', '-xvzf', dFile,'-C', os.path.expanduser('~')], stdout=open('%s.logfile' % depends, 'w') )
	print "Cd'ing to ~/songbird/trunk"
	os.chdir(os.path.expanduser('~/songbird/trunk'))
	print " Making songbird.mk #1"
	os.system('make -f songbird.mk > error 2>&1')
	print " cd'ing to ~/songbird/trunk/compiled/dist"
	os.chdir(os.path.expanduser('~/songbird/trunk/compiled/dist/'))
	print " running ./Songbird"
	os.system("./Songbird > error 2>&1")
	print "running making songbird.mk clobber"
	os.system('make -f songbird.mk clobber > error 2>&1')
	print "running make -f songbird.mk #2"
	os.system('make -f songbird.mk > error 2>&1')
	print "congrats it worked and you should now be able to run songbird"
	raw_input("\n\nhit enter key to exit.")
if __name__ == '__main__':
	main()